<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
    <title>数据字典管理</title>
    <meta name="decorator" content="default"/>
    <script type="text/javascript">
        $(document).ready(function() {
            //$("#name").focus();
            $("#inputForm").validate({
                submitHandler: function(form){
                    loading('正在提交，请稍等...');
                    form.submit();
                },
                errorContainer: "#messageBox",
                errorPlacement: function(error, element) {
                    $("#messageBox").text("输入有误，请先更正。");
                    if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
                        error.appendTo(element.parent().parent());
                    } else {
                        error.insertAfter(element);
                    }
                }
            });
        });
        function addRow(list, idx, tpl, row){
            $(list).append(Mustache.render(tpl, {
                idx: idx, delBtn: true, row: row
            }));
            $(list+idx).find("select").each(function(){
                $(this).val($(this).attr("data-value"));
            });
            $(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
                var ss = $(this).attr("data-value").split(',');
                for (var i=0; i<ss.length; i++){
                    if($(this).val() == ss[i]){
                        $(this).attr("checked","checked");
                    }
                }
            });
        }
        function delRow(obj, prefix){
            var id = $(prefix+"_id");
            var delFlag = $(prefix+"_delFlag");
            if (id.val() == ""){
                $(obj).parent().parent().remove();
            }else if(delFlag.val() == "0"){
                delFlag.val("1");
                $(obj).html("&divide;").attr("title", "撤销删除");
                $(obj).parent().parent().addClass("error");
            }else if(delFlag.val() == "1"){
                delFlag.val("0");
                $(obj).html("&times;").attr("title", "删除");
                $(obj).parent().parent().removeClass("error");
            }
        }
    </script>
</head>
<body>
    <ul class="nav nav-tabs">
        <li><a href="${ctx}/pm/pmDataDict/">数据字典列表</a></li>
        <li class="active"><a href="${ctx}/pm/pmDataDict/form?id=${pmDataDict.id}">数据字典<shiro:hasPermission name="pm:pmDataDict:edit">${not empty pmDataDict.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="pm:pmDataDict:edit">查看</shiro:lacksPermission></a></li>
    </ul><br/>
    <form:form id="inputForm" modelAttribute="pmDataDict" action="${ctx}/pm/pmDataDict/save" method="post" class="form-horizontal">
        <form:hidden path="id"/>
        <sys:message content="${message}"/>        
        <div class="control-group">
            <label class="control-label">字典标识：</label>
            <div class="controls">
                <form:input path="dictCode" htmlEscape="false" maxlength="30" class="input-xlarge required"/>
                <span class="help-inline" style="color:red">*</span>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">字典名称：</label>
            <div class="controls">
                <form:input path="dictName" htmlEscape="false" maxlength="30" class="input-xlarge required"/>
                <span class="help-inline" style="color:red">*</span>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">是否允许删除：</label>
            <div class="controls">
                <form:radiobuttons path="allowDelete" items="${fns:getDataDictItemList('yes_no')}" itemLabel="itemName" itemValue="itemCode" htmlEscape="false" class="required"/>
                <span class="help-inline" style="color:red">*</span>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">最少条目数：</label>
            <div class="controls">
                <form:input path="minItems" htmlEscape="false" maxlength="30" class="input-xlarge required"/>
                <span class="help-inline" style="color:red">*</span>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">最大条目数：</label>
            <div class="controls">
                <form:input path="maxItems" htmlEscape="false" maxlength="30" class="input-xlarge required"/>
                <span class="help-inline" style="color:red">*</span>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">备注信息：</label>
            <div class="controls">
                <form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="255" class="input-xxlarge "/>
            </div>
        </div>
            <div class="control-group">
                <label class="control-label">字典条目：</label>
                <div class="controls">
                    <table id="contentTable" class="table table-striped table-bordered table-condensed">
                        <thead>
                            <tr>
                                <th class="hide"></th>
                                <th>编码</th>
                                <th>值</th>
                                <th>排序</th>
                                <th>备注</th>
                                <shiro:hasPermission name="pm:pmDataDict:edit"><th width="10">&nbsp;</th></shiro:hasPermission>
                            </tr>
                        </thead>
                        <tbody id="pmDataDictItemList">
                        </tbody>
                        <shiro:hasPermission name="pm:pmDataDict:edit"><tfoot>
                            <tr><td colspan="6"><a href="javascript:" onclick="addRow('#pmDataDictItemList', pmDataDictItemRowIdx, pmDataDictItemTpl);pmDataDictItemRowIdx = pmDataDictItemRowIdx + 1;" class="btn">新增</a></td></tr>
                        </tfoot></shiro:hasPermission>
                    </table>
                    <script type="text/template" id="pmDataDictItemTpl">//<!--
                        <tr id="pmDataDictItemList{{idx}}">
                            <td class="hide">
                                <input id="pmDataDictItemList{{idx}}_id" name="pmDataDictItemList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
                                <input id="pmDataDictItemList{{idx}}_delFlag" name="pmDataDictItemList[{{idx}}].delFlag" type="hidden" value="0"/>
                            </td>
                            <td>
                                <input id="pmDataDictItemList{{idx}}_itemCode" name="pmDataDictItemList[{{idx}}].itemCode" type="text" value="{{row.itemCode}}" maxlength="30" class="input-small required"/>
                            </td>
                            <td>
                                <input id="pmDataDictItemList{{idx}}_itemName" name="pmDataDictItemList[{{idx}}].itemName" type="text" value="{{row.itemName}}" maxlength="255" class="input-small "/>
                            </td>
                            <td>
                                <input id="pmDataDictItemList{{idx}}_sort" name="pmDataDictItemList[{{idx}}].sort" type="text" value="{{row.sort}}" maxlength="11" class="input-small "/>
                            </td>
                            <td>
                                <input id="pmDataDictItemList{{idx}}_remarks" name="pmDataDictItemList[{{idx}}].remarks" type="text" value="{{row.remarks}}" maxlength="11" class="input-small "/>
                            </td>
                            <shiro:hasPermission name="pm:pmDataDict:edit"><td class="text-center" width="10">
                                {{#delBtn}}<span class="close" onclick="delRow(this, '#pmDataDictItemList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
                            </td></shiro:hasPermission>
                        </tr>//-->
                    </script>
                    <script type="text/javascript">
                        var pmDataDictItemRowIdx = 0, pmDataDictItemTpl = $("#pmDataDictItemTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
                        $(document).ready(function() {
                            var data = ${fns:toJson(pmDataDict.pmDataDictItemList)};
                            for (var i=0; i<data.length; i++){
                                addRow('#pmDataDictItemList', pmDataDictItemRowIdx, pmDataDictItemTpl, data[i]);
                                pmDataDictItemRowIdx = pmDataDictItemRowIdx + 1;
                            }
                        });
                    </script>
                </div>
            </div>
        <div class="form-actions">
            <shiro:hasPermission name="pm:pmDataDict:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
            <input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
        </div>
    </form:form>
</body>
</html>